package Druid;


import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DruidDemo {
    //定义连接池对象
    private  static DataSource dataSource;
    static {
        try {
            //创建持久化属性集合
        Properties pro = new Properties();
        //通过类加载器,加载durid.properties文件   因为这个类加载器默认是从src路径下去找资源所以不用写src路径
        InputStream fis = DruidDemo.class.getClassLoader().getResourceAsStream("durid.properties");
//        pro.load(new FileInputStream("src/durid.properties"));
            pro.load(fis);
           dataSource = DruidDataSourceFactory.createDataSource(pro);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void deleteUser() throws SQLException {
        //从连接池中获取连接
        Connection con = dataSource.getConnection();
        //获取可执行对象
        Statement st = con.createStatement();
        String sql ="delete from user where uid = 3";
        //执行并获取结果
        System.out.println(st.executeUpdate(sql) >0? "删除成功":"删除失败");
        st.close();
        con.close();

    }
}
